<?php

namespace App\Http\Controllers\Asset;

use App\Http\Controllers\Controller;
use App\Http\Resources\AssetResource;
use App\Models\AssetStock;
use Illuminate\Http\Request;
use function _\find;

class AssetStockController extends Controller
{
    use AssetFindOrNewPivotTrait;

    public function getAssetStock($id)
    {
        $result = AssetStock::find($id);
        return response()->json(compact(['result']));
    }

    public function delete($id)
    {
        $result = [
            "success" => true,
            "message" => ""
        ];
        AssetStock::whereId($id)->delete();
        return response()->json(compact(['result']));
    }

    public function store(Request $request)
    {
        $request->validate(
            [
                'name' => 'required',
                'brand' => 'required',
                'affiliation' => 'required',
                'keeper' => 'required'
            ],
            [
                'name.required' => '名称字段不能为空',
                'brand.required' => '品牌不能为空',
                'affiliation.required' => '归属不能为空',
                'keeper.required' => '保管人不能为空'
            ]);
        $assetId = request()->get('id');
        $assetStock = AssetStock::findOrNew($assetId);
        $assetStock->name = request()->get('name');
        $assetStock->product_model_number = request()->get('product_model_number');
        $assetStock->category_id = request()->get('category_id');
        $assetStock->sub_category_id = request()->get('sub_category_id');
        $assetStock->init_quantity = request()->get('init_quantity');
        $assetStock->marking_price_per_unit = request()->get('marking_price_per_unit') ?? 0;
        $assetStock->bid_price_per_unit = request()->get('bid_price_per_unit') ?? 0;
        $assetStock->description = request()->get('description');
        $assetStock->affiliation_id = $this->getOrNewPivot('affiliation');
        $assetStock->keeper_id =$this->getOrNewPivot('keeper');
        $assetStock->brand_id = $this->getOrNewPivot('brand');
        if(empty($assetId)) {
            $assetStock->total_quantity = request()->get('init_quantity');
            $assetStock->available_quantity = request()->get('init_quantity');
        }
        // optional
        $assetStock->vendor_id = $this->getOrNewPivot('vendor');
        $assetStock->location_id = $this->getOrNewPivot('location');
        $assetStock->save();
        // refresh the related pivot tables
        $assetStock->refresh();
        return response()->json(compact(['assetStock']));
    }

    public function list()
    {
        $result = new AssetResource(collect());
        $itemsPerPage = request()->get('itemsPerPage');
        $page = request()->get('page');
        $sortBy = request()->get('sortBy');
        $query = request()->get('query');

        $AssetStockQuery = AssetStock::query();
        if(!empty($query)) {
            $AssetStockQuery->fuzzyFilter($query);
        }
        if (!empty($sortBy)) {
            $orderByColumn = $sortBy[0];
            $sortDesc = request()->get('sortDesc');
            $sortType = $sortDesc[0] ? 'desc' : 'asc';
            $AssetStockQuery->orderBy($orderByColumn, $sortType);
        } else {
            $AssetStockQuery->orderBy('id', 'desc');
        }
        $result = new AssetResource($AssetStockQuery->paginate($itemsPerPage));
        return $result;
    }
}
